<?php
/**
 * @version		$Id: coupons.php 3 2011-12-16 20:19:15Z netix.mail@gmail.com $
 * @package		ALM Hoster
 * @desription	Web hosting and domain menager for resellers
 * @subpackage	Components
 * @copyright	Copyright (C) 2011 ALM Studio. All rights reserved.
 * @author		ALM Studio
 * @email		info@alm-studio.net
 * @link		http://alm-studio.net
 * @license		Commercial
 */

// No direct access to this file
defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.modellist');

class AlmHosterModelCoupons extends JModelList
{

	public function __construct($config = array())
	{
		if (empty($config['filter_fields'])) {
			$config['filter_fields'] = array(
				'CouponId', 'a.CouponId',
				'CouponName', 'a.CouponName',
				'CouponDiscountType', 'a.CouponDiscountType',
				'CouponHostingDisc', 'a.CouponHostingDisc',
				'CouponDomainDisc', 'a.CouponDomainDisc',
				'CouponValidFrom', 'a.CouponValidFrom',
				'CouponValidTo', 'a.CouponValidTo',
				'CouponUsed', 'a.CouponUsed',
				'CouponStatus', 'a.CouponStatus'
			);
		}

		parent::__construct($config);
	}
	
	protected function populateState($ordering = null, $direction = null)
	{

		$app = JFactory::getApplication('administrator');
		
		$filters = JRequest::getVar('filters');
		if (empty($filters)) {
			$data = $app->getUserState($this->context.'.data');
			$filters = $data['filters'];
		}
		else {
			$app->setUserState($this->context.'.data', array('filters'=>$filters));
		}

		$search = $this->getUserStateFromRequest($this->context.'.filter.search', 'filter_search');
		$this->setState('filter.search', $search);
		
		$statusId = $app->getUserStateFromRequest($this->context.'.filter.status_id', 'filter_status_id');
		$this->setState('filter.status_id', $statusId);
		
		
		parent::populateState('a.CouponId', 'desc');		
				
	}

	
	protected function getListQuery() 
	{
		$db = JFactory::getDBO();
		$query = $db->getQuery(true);
		
		
		$query->select($this->getState('list.select', 'a.*'));
		
		$query->from('#__almh_coupons AS a');
		
		
		$statusId = $this->getState('filter.status_id');
		if (is_numeric($statusId)) {
			$query->where('a.CouponStatus = '.$statusId);
		}
		
		$search = $this->getState('filter.search');
		if (!empty($search)) {
			if (stripos($search, 'CouponName:') === 0) {
				$query->where('a.CouponName = '.(int) substr($search, 3));
			} else {
				$search = $db->Quote('%'.$db->getEscaped($search, true).'%');
				$query->where('(a.CouponName LIKE '.$search.')');
			}
		}
		
		$query->order($db->getEscaped($this->getState('list.ordering', 'a.CouponId')).' '.$db->getEscaped($this->getState('list.direction', 'desc')));
			
		return $query;		
	}
	
	
	public function getForm($data = array(), $loadData = true)
	{
		jimport('joomla.form.form');
		$app = JFactory::getApplication();
		JForm::addFormPath(JPATH_COMPONENT . '/models/forms');
		JForm::addFieldPath(JPATH_COMPONENT . '/models/fields');
		$form = JForm::getInstance('com_almhoster.coupons', 'coupons', array('load_data' => $loadData));

		if ($form == false) {
			$this->setError($form->getMessage());
			return false;
		}
		$data = $this->loadFormData();

		if (!empty($data)) {
			$form->bind($data);
		}

		return $form;
	}
	
	
	protected function loadFormData()
	{
		$data = JFactory::getApplication()->getUserState('com_almhoster.coupons.data', array());

		return $data;
	}

}